<template>
	<view class="main">
		<zmm-watermark :watermark="watermark"></zmm-watermark>
		<view class="header">
			<view class="left">
				<!-- <view class="img">
					<image :src="userInfo.avatar" mode=""></image>
				</view> -->
				<view class="msg">
					Hi， {{userInfo.nickname}}
				</view>
			</view>
			<view class="time">{{today}}</view>
			<!-- <view class="btn"
				@tap="routerTo(`/pages/pageB/data/clockIn?show=${show1}`)">去打卡</view> -->
		</view>
		<view class="oneBox">
			<view class="in">
				<h2>待办事项</h2>
				<view class="itemBox">
					<view class="item" @tap="routerTo('/pages/index/dbOrder?type=1')">
						<p class="tit">派送订单</p>
						<p class="num">{{num1}}</p>
						<p class="btn">去完成</p>
					</view>
					<view class="item" @tap="routerTo('/pages/index/dbOrder?type=2')">
						<p class="tit">疑难订单</p>
						<p class="num">{{num2}}</p>
						<p class="btn">去完成</p>
					</view>
					<view class="item" @tap="routerTo('/pages/index/dbOrder?type=3')">
						<p class="tit">在途订单</p>
						<p class="num">{{num3}}</p>
						<p class="btn">去完成</p>
					</view>
				</view>
			</view>
		</view>
		<ul class="list">
			<li class="item one" @tap="routerTo('/pages/pageB/data/profitLoss?type=1')">
				<view class="num">
					{{total1}}
				</view>
				<view class="msg">今日盈利</view>
			</li>
			<li class="item one" @tap="routerTo('/pages/pageB/data/profitLoss?type=2')">
				<view class="num">
					{{total2}}
				</view>
				<view class="msg">今日退单费</view>
			</li>
			<li class="item" @tap="routerTo('/pages/innerOrder/innerOrder')">
				<image src="@/static/img/jryl.png" mode=""></image>
				<view class="msg">订单</view>
			</li>
			<li class="item" v-if="userInfo.is_manager == 1 || userInfo.is_leader == 1"
				@tap="routerTo('/pages/pageB/data/statistics')">
				<image src="@/static/img/cwtj.png" mode=""></image>
				<view class="msg">财务统计</view>
			</li>
			<li class="item" v-if="userInfo.is_super != 1" @tap="routerTo('/pages/pageB/data/revenue')">
				<image src="@/static/img/gxsz.png" mode=""></image>
				<view class="msg">各项收支</view>
			</li>
			<li class="item" @tap="routerTo('/pages/pageB/data/cashout')">
				<image src="@/static/img/tx.png" mode=""></image>
				<view class="msg">提现</view>
			</li>
			<li class="item" @tap="routerTo('/pages/pageB/data/censece')">
				<image src="@/static/img/tx.png" mode=""></image>
				<view class="msg">假勤统计</view>
			</li>
			<li class="item" @tap="routerTo('/pages/pageB/data/manage')">
				<image src="@/static/img/tx.png" mode=""></image>
				<view class="msg">假勤管理</view>
			</li>
			<li class="item" v-if="userInfo.is_leader == 1  || userInfo.is_audit == 1" @tap="routerTo('/pages/pageB/data/approve')">
				<image src="@/static/img/tx.png" mode=""></image>
				<view class="msg">假勤审批</view>
			</li>
			<li class="item" v-if="userInfo.is_need_sign == 1"
				@tap="routerTo(`/pages/pageB/data/clockIn?show=${show1}`)">
				<image src="@/static/img/clock.png" mode=""></image>
				<view class="msg">打卡</view>
			</li>
		</ul>
		<u-modal :show="show" :content="content" @confirm="downLoad" confirmButtonShape="circle"></u-modal>
		<u-modal :show="show1" confirmText="去打卡" @confirm="routerTo('/pages/pageB/data/clockIn')"
			:closeOnClickOverlay="true" @close="show1 = false">
			<view class="slot-content">
				<view class="modalCon">
					<image class="close" src="@/static/img/close.png" mode="" @tap="show1 = false"></image>
					<image class="bg" src="@/static/img/clockBg.png" mode=""></image>
					<view class="time">
						{{ nowClock }}
					</view>
					<view class="msg">你还未打卡</view>
				</view>
			</view>
		</u-modal>
		<innerTabbar seleTabbar="4"></innerTabbar>
	</view>
</template>

<script>
	import {
		innerinfo,
		Productorder,
		isSign,
		signTimes,
		todayClock,
		Productlist
	} from '@/api/inner.js';
	import {
		getCates,
		indexList,
		getConfigs
	} from '@/api/user.js';
	import innerTabbar from '../../components/innerTabbar.vue';
	export default {
		components: {
			innerTabbar
		},
		data() {
			return {
				userInfo: {},
				total1: '',
				total2: '',
				watermark: '',
				show: false,
				show1: false,
				content: '您的版本不是最新版，请更新后使用',
				version: '',
				nowClock: '',
				isClock: 1,
				today: '',
				data: {
					page_size: 15,
					start_date: '', //开始日期
					end_date: '', //结束日期
					status: '',
					department_user_id: '',
					page: 1
				},
				num1: '',
				num2: '',
				num3: ''
			};
		},
		onLoad() {
			let now = new Date()
			let y = now.getFullYear()
			let m = now.getMonth() + 1
			let d = now.getDate()
			let dd = now.getDay()
			dd = this.toChineseNumber(dd)
			this.today = `${m}月${d}日 星期${dd}`
			/*#ifdef APP-PLUS*/
			// const systemInfo = uni.getSystemInfoSync()
			// if (systemInfo.platform === 'android'){
			// 	uni.getSystemInfo({
			// 		success: (res) => {
			// 			this.version = res.appWgtVersion;
			// 			getConfigs({
			// 				name: 'version'
			// 			}).then((res) => {
			// 				if (this.version != res.data.value) {
			// 					this.show = true;
			// 				} else {

			// 				}
			// 			});
			// 		}
			// 	});
			// }else{
			// 	this.getInfo();
			// 	this.getList(1);
			// 	this.getList(2);
			// 	this.getOrder(12)
			// 	this.getOrder('15,16,17')
			// 	this.getOrder(11)
			// 	this.watermark = uni.getStorageSync('watermark')
			// }
			this.getInfo();
			this.getList(1);
			this.getList(2);
			this.getOrder(12)
			this.getOrder('15,16,17')
			this.getOrder(11)
			/*#endif*/
			/*#ifdef H5*/
			this.getInfo();
			this.getList(1);
			this.getList(2);
			this.getOrder(12)
			this.getOrder('15,16,17')
			this.getOrder(11)
			/*#endif*/
		},
		onShow() {

		},
		methods: {
			async getOrder(status) {
				this.data.status = status
				let res = await Productlist(this.data)
				if (status == 12) {
					this.num1 = res.data.total
				} else if (status == 11) {
					this.num3 = res.data.total
				} else {
					this.num2 = res.data.total
					
					/*#ifdef APP-PLUS*/
					// console.log(this.num2)
					if (this.num2 == 0) {
						plus.runtime.setBadgeNumber(-1);
					} else {
						plus.runtime.setBadgeNumber(this.num2);
					}
					/*#endif*/
				}
			},
			toChineseNumber(num) {
				const chineseNumbers = ['日', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
				return chineseNumbers[num];
			},
			async clockTime() {
				let res = await signTimes();
				let timeArr = res.data;
				let date = new Date();
				let y = date.getFullYear();
				let m = date.getMonth() + 1;
				let d = date.getDate();
				let arr = [];
				timeArr.forEach((item, index) => {
					let time1 = `${y}-${m}-${d} ${item[0]}`;
					let time2 = `${y}-${m}-${d} ${item[1]}`;
					let a1 = new Date(time1).getTime();
					let a2 = new Date(time2).getTime();
					arr.push([a1, a2]);
				});
				let now = new Date().getTime();
				let nowTime = new Date();
				let hh = nowTime.getHours();
				let mm = nowTime.getMinutes();
				mm = mm < 10 ? '0' + mm : mm
				this.nowClock = `${hh}:${mm}`;
				arr.forEach((item, index) => {
					if (now > item[0] && now < item[1]) {
						if (this.isClock == 0) {
							this.show1 = true;
							return;
						}
					}
				});
			},
			downLoad() {
				uni.getSystemInfo({
					success: function(res) {
						uni.showLoading({
							title: '下载中'
						});
						uni.downloadFile({
							url: `https://app.guoshangufen.com/gsyy.apk`,
							success: (res) => {
								console.log(res, '下载成功');
								if (res.statusCode === 200) {
									console.log('下载成功');
									uni.hideLoading();
									uni.showToast({
										title: '下载成功',
										icon: 'success'
									});
									uni.saveFile({
										tempFilePath: res.tempFilePath,
										success: function(res) {
											uni.openDocument({
												filePath: res.savedFilePath,
												success: function(res) {
													console.log(res,
														'打开安装包');
												}
											});
										},
										fail: (err) => {
											console.log(err, '打开安装包-失败');
										}
									});
								}
							},
							fail: (err) => {
								console.log(err, '下载失败');
								uni.hideLoading();
								uni.showToast({
									title: '下载失败,请检查网络',
									icon: 'none',
									duration: 1500
								});
							}
						});
					}
				});
			},
			async getInfo() {
				let res = await innerinfo();
				console.log(res)
				this.userInfo = res.data;
				uni.setStorageSync('userid', res.data.id)
				uni.setStorageSync('userinfo', res.data)
				uni.setStorageSync('watermark', res.data.username)
				this.watermark = uni.getStorageSync('watermark')
				if (this.userInfo.is_need_sign == 1) {
					todayClock().then(res => {
						if (res.data) {
							this.clockTime();
							isSign().then((res) => {
								this.isClock = res.data.status;
							});
						}
					})
				}
			},
			async getList(type) {
				let res = await Productorder({
					page_size: 15,
					type: type,
					department_user_id: '', //员工id
					department_id: '' //部门id
				});
				this.list = res.data.list.data;
				if (type == 1) {
					this.total1 = res.data.total;
				} else {
					this.total2 = res.data.total;
				}
			},
			routerTo(url) {
				uni.navigateTo({
					url: url
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.main {
		min-height: calc(100vh - 50px);
		height: fit-content;
		padding: 0 0 40px;
		background-color: #f8f8f8;

		.slot-content {
			.modalCon {
				display: flex;
				flex-direction: column;
				align-items: center;
				position: relative;

				.time {
					color: #0a84ff;
					font-size: 50px;
					margin-bottom: 10px;
				}

				.msg {
					color: #4c83d2;
				}

				.close {
					width: 35px;
					height: 35px;
					position: absolute;
					top: -10px;
					right: -55px;
				}

				.bg {
					width: 207px;
					height: 117px;
				}
			}
		}

		.header {
			justify-content: space-between;
			padding: 60px 25px 30px;
			background-color: #0a80f8;
			border-radius: 0 0 20px 20px;
			display: flex;
			align-items: center;
			color: #fff;
			margin-bottom: 15px;

			.left {
				display: flex;
				align-items: center;

				.img {
					border: 2px solid #fff;
					border-radius: 5px;
					margin-right: 10px;

					image {
						width: 34px;
						height: 34px;
						border-radius: 5px;
					}
				}

				.msg {
					font-size: 14px;
					color: #fff;
				}
			}

			.btn {
				padding: 5px 15px;
				font-size: 12px;
				color: #fff;
				border: 1px solid #ffffff;
				background-color: #78b0fa;
				border-radius: 100px;
			}
		}

		.oneBox {
			width: 100%;
			padding: 0 18px;
			margin-bottom: 15px;

			.in {
				padding: 10px 15px;
				background-color: #fff;
				border-radius: 10px;

				h2 {
					font-size: 16px;
					font-weight: 700;
					margin-bottom: 10px;
				}

				.itemBox {
					display: flex;
					justify-content: space-between;

					.item {
						width: 30%;
						padding: 10px;
						background-color: #f5f8ff;
						border-radius: 5px;

						.tit {
							font-size: 20;
							font-weight: 700;
							margin-bottom: 8px;
						}

						.num {
							font-size: 14px;
							margin-bottom: 8px;
							color: red;
						}

						.btn {
							display: inline-block;
							font-size: 10px;
							border-radius: 1000px 1000px 1000px 0;
							background-color: #0a80f8;
							color: #fff;
							padding: 1px 5px 1px 3px;
						}
					}
				}
			}

		}

		.list {
			padding: 0 18px;
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;

			.item {
				width: 32%;
				height: 100px;
				background-color: #fff;
				border-radius: 5px;
				padding: 10px 0;
				margin-bottom: 15px;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				font-size: 12px;
				color: #3d3d3d;

				image {
					width: 35px;
					height: 35px;
					margin-bottom: 10px;
				}

				.num {
					font-size: 25px;
					font-weight: 700;
					color: #0a80f8;
					margin-bottom: 10px;
				}
			}

			.one {
				width: 48%;
			}
		}
	}
</style>